<template>
    <div>
        <el-form :rules="rules" ref="loginForm" :model="loginForm" class="loginContainer" @submit.native.prevent>
            <h3 class="loginTitle">系统登录</h3>
            <el-form-item prop="username">
                <el-input type="text" v-model="loginForm.username" placeholder="请输入用户名"></el-input>
            </el-form-item>
            <el-form-item prop="password">
                <el-input type="password" v-model="loginForm.password" placeholder="请输入密码"></el-input>
            </el-form-item>
            <el-checkbox v-model="checked" class="loginRemember">记住我</el-checkbox>
            <el-button type="primary" style="width: 100%;" @click="submitLogin" native-type="submit">登录</el-button>
        </el-form>
    </div>
</template>

<script>

    export default {
        name: "Login",
        data() {
            return {
                loginForm: {
                    username: "admin",
                    password: "123456"
                },
                checked: true,
                loading: false,
                count: '',
                show: '',
                rules: {
                    username: [{required: true, message: '请输入用户名', trigger: 'blur'}],
                    password: [{required: true, message: '请输入密码', trigger: 'blur'}]
                }
            }
        },
        methods: {
            submitLogin() {
                this.$refs.loginForm.validate(async valid => {
                    if (valid) {
                        const {data:res} = await this.$http.post('login', this.loginForm);
                        // console.log(res);
                        if (res.meta.status == 200) {
                            window.sessionStorage.setItem('token', res.data.token);
                            this.$message({
                                message: res.meta.msg,
                                type: 'success',
                                center: true
                            });
                            const timejump = 1;
                            if (!this.timer) {
                                this.count = timejump;
                                this.show = false;
                                this.timer = setInterval(() => {
                                    if (this.count > 0 && this.count <= timejump) {
                                        this.count--;
                                    } else {
                                        this.show = true;
                                        clearInterval(this.timer);
                                        this.timer = null;
                                        //跳转的页面写在此处
                                        this.$router.replace('/home');
                                    }
                                }, 1800)
                            }
                        } else {
                            this.$message.error({
                                message: res.meta.msg,
                                center: true
                            });
                        }

                        // alert(result.meta.msg);

                        // postRequest('/api/login',this.loginForm).then(resp=>{
                        // 	if (resp){
                        // 		this.loading = false;
                        // 		this.$router.replace('/home');
                        // 	}
                        // })
                    } else {
                        console.log('error');
                        return false;
                    }
                })
            }
        }
    }
</script>

<style scoped>
    .loginContainer {
        border-radius: 15px;
        background-clip: padding-box;
        margin: 180px auto;
        width: 350px;
        padding: 15px 35px 15px 35px;
        background: #ffffff;
        border: 1px solid #eaeaea;
        box-shadow: 0 0 25px #cac6c6;
    }

    .loginTitle {
        margin: 0 auto 40px auto;
        text-align: center;
    }

    .loginRemember {
        text-align: left;
        margin: 0 0 15px 8px;
    }
</style>>
